﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Light.Framework.Model;
using Light.Framework.Contract;
using Light.Framework.Castle;
using NHibernate.Criterion;

namespace Light.Framework.Component
{
    public class TaskComponent : BaseComponent<Task>, ITaskComponent
    {
        #region ITaskComponent 成员

        public Task GetTaskByCode(string code)
        {
            ICriterion criterion = Expression.Eq("Code", code);
            criterion = Expression.And(criterion, base.GetNotDeletedCriterion());

            return EntityMediator<Task>.FindOneOrDefault(criterion);
        }

        public override IList<Task> GetAll()
        {
            ModelOrder.Clear();
            ModelOrder.Add("DisplayOrder", true);

            ICriterion criterion = base.GetNotDeletedCriterion();
            if (!ServerApplication.User.IsSuper)
            {
                criterion = Expression.And(criterion, Expression.Eq("Status", Status.Normal));
            }
            return base.GetAll(criterion);
        }

        protected override ICriterion CreateCriterion(Task model)
        {
            ICriterion criterion = Expression.Eq("Code", model.Code);
            criterion = Expression.And(criterion, base.GetNotDeletedCriterion());

            return criterion;
        }

        protected override Func<Task, bool> CreateFunc(Task model)
        {
            return t => t.Code == model.Code && !t.IsDeleted;
        }

        protected override string GetExistMessage(Task model)
        {
            return "已存在权限 " + model.Code;
        }

        #endregion
    }
}
